/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.DepartmentDAO;
import exatrez.manager.DepartManager;
import exatrez.model.Department;
import exatrez.model.InfoAudit;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminDepartment")
@AuthRequired
public class DepartmentAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(DepartmentAction.class);
    private Department depart;
    private List<Department> listOfDepartment;
    @Inject
    private DepartManager departManager;
    @Inject
    private DepartmentDAO departmentDAO;

    @DefaultHandler
    public Resolution add_department() {
        depart = new Department();
        listOfDepartment = departManager.findAll();
        return new JSP("admin/department.jsp");
    }

    public Resolution saveDepartment() {

        LOG.debug("masuk");
        Department d = departManager.findDepartmentName(depart.getDepartmentName());
        if (d == null) {
            User user = getContext().getCurrentUser();
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            depart.setInfoAudit(ia);
            depart.setActive('Y');
            depart.setShipVisitProcessFlow("T");    // T = True (Need Approval), F = False (Skip Approval)
            departManager.saveOrUpdate(depart);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Department!");
        }

        return add_department();
    }

    public Resolution deleteDepartment() {
        String departmentId = getContext().getRequest().getParameter("departmentId");
        if (departmentId != null) {
            depart = departmentDAO.findById(Integer.parseInt(departmentId));
            if (depart != null) {
                departManager.deleteDepartment(depart);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(DepartmentAction.class, "add_department");
    }

    public Resolution popupEditDepartment() {
        String departmentId = getContext().getRequest().getParameter("departmentId");
        if (departmentId != null) {
            depart = departmentDAO.findById(Integer.parseInt(departmentId));
        }
        return new JSP("admin/edit_department.jsp").addParameter("popup", "true");
    }

    public Resolution saveEditDepartment() {
        String departmentId = getContext().getRequest().getParameter("depart.departmentId");
        if (departmentId != null) {
            User user = getContext().getCurrentUser();
            Department d = departmentDAO.findById(Integer.parseInt(departmentId));
            if (!depart.getDepartmentName().equals(d.getDepartmentName())) {
                Department d1 = departManager.findNotDepartmentName(String.valueOf(d.getDepartmentId()), depart.getDepartmentName());
                if (d1 != null) {
                    addSimpleError("Duplicate Department Name!");
                    return add_department();
                } else {
                    d.setDepartmentName(depart.getDepartmentName());
                }
            }
            InfoAudit ia = d.getInfoAudit();
            d.setTarget(depart.getTarget());
            d.setActive(depart.getActive());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            d.setInfoAudit(ia);
            departManager.saveOrUpdate(d);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(DepartmentAction.class, "add_department");
    }

    public Department getDepart() {
        return depart;
    }

    public void setDepart(Department depart) {
        this.depart = depart;
    }

    public DepartManager getDepartManager() {
        return departManager;
    }

    public void setDepartManager(DepartManager departManager) {
        this.departManager = departManager;
    }

    public List<Department> getListOfDepartment() {
        return listOfDepartment;
    }

    public void setListOfDepartment(List<Department> listOfDepartment) {
        this.listOfDepartment = listOfDepartment;
    }

    public DepartmentDAO getDepartmentDAO() {
        return departmentDAO;
    }

    public void setDepartmentDAO(DepartmentDAO departmentDAO) {
        this.departmentDAO = departmentDAO;
    }
}
